package com.google.code.sysarch.v1.e31;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.google.code.sysarch.common.util.Parameter;

/* Starts the app and inits the GUI */
@SuppressWarnings("serial")
public class v1_31 extends JPanel implements ActionListener {
	
	static final int FrameWidth = 500;
	private static Dimension screenSize;
	private static Parameter arguments;
	private static int numBars = 0;
	private int nextBar = 0;
    private JProgressBar[] progressBar;
    private JButton startButton;
    private JButton exitButton;
	
    /*
     * Constructor that inits our GUI
     */
    public v1_31() {
    	super(new GridLayout(numBars+2, 1, 5, 5));
        
        startButton = new JButton("Start");
        startButton.setActionCommand("start");
        startButton.addActionListener(this);
        add(startButton);        
        
        progressBar = new JProgressBar[numBars];
        for (int i=0; i<numBars; i++) {
	        progressBar[i] = new JProgressBar(0, 100);
	        progressBar[i].setValue(0);
	        progressBar[i].setStringPainted(true);
	        add(progressBar[i]);
        }
        
        exitButton = new JButton("exit");
        exitButton.setActionCommand("exit");
        exitButton.addActionListener(this);
        add(exitButton);        
        
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    }
    
    /*
     * Invoked when the user presses a button.
     */
    public void actionPerformed(ActionEvent evt) {
    	if (evt.getActionCommand().equals("start")) {
	        startButton.setEnabled(false);
			Scheduler schedulerThread = new Scheduler(arguments, this);	
			schedulerThread.setPriority(Thread.MAX_PRIORITY);
			schedulerThread.start();
    	} else if (evt.getActionCommand().equals("exit")) {
    		System.exit(0);
    	}
    }    
    
    /*
     * Updates the progressBar, is called by the scheduler
     */
    public JProgressBar getBar() {
    	if (nextBar < numBars) {
    		nextBar++;
    		return progressBar[nextBar - 1];
    	}
    	return null;
    }
	
    /*
     * Creates and displays the GUI
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("User Level Scheduler");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new v1_31();
        newContentPane.setOpaque(true); //content panes must be opaque
        newContentPane.setPreferredSize(new Dimension(v1_31.FrameWidth, numBars*40 + 60));
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.setLocation(screenSize.width/2 - FrameWidth/2, screenSize.height/2 - numBars*20 - 30);
        frame.pack();
        frame.setVisible(true);
    }	

	/* main function creates a scheduler */
	public static void main(String[] args) {
		try {
	        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	        //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
	    } 
	    catch (UnsupportedLookAndFeelException e) { }
	    catch (ClassNotFoundException e) { }
	    catch (InstantiationException e) { }
	    catch (IllegalAccessException e) { }		
	    screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		arguments = new Parameter(args);
		numBars = arguments.getInt("n", 5, 1, 10);
		
	    //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
	}

}
